package com.openxt.nbzj.util;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import java.io.File;

import qs.openxt.libs.util.StringUtil;

/**
 * Created by 颖杰_2 on 2016-11-25.
 */

public class FileOpenUtil {

    /**
     * 打开文件
     *
     * @param file
     */
    public static void openFile(Activity mActivity, File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
        String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (StringUtil.isNull(extension) || StringUtil.isNull(mimetype)) {
            // if there is no extension or there is no definite mimetype, still try to open the file
            intent.setDataAndType(Uri.fromFile(file), "text/*");
        } else {
            intent.setDataAndType(Uri.fromFile(file), mimetype);
        }
        // custom message for the intent
        try {
            mActivity.startActivity(Intent.createChooser(intent, "选择一个应用程序:"));
        }catch (Exception e){
            Toast.makeText(mActivity, "系统中尚未安装处理这种类型的应用.", Toast.LENGTH_LONG).show();
        }
    }
}
